package callback.syn.example3;

import java.util.ArrayList;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.testng.annotations.Test;

/**
 *
 *
 */
public class Event {

    /**
     * 将方法作为参数传递的两种方法，lambda，方法引用
     *
     */

    private <T> void printANumber(int number, Consumer<? super T> function) {
        System.out.println("The number you provided is: " + number);
    }

    private void printANumber11(int number, Consumer<Void> function) {
        System.out.println("The number you provided is: " + number);
        function.accept(null);
    }

    private <T> void printANumber2(int number, Supplier<? super T> function) {
        System.out.println("The number you provided is: " + number);
    }

    private void printANumber22(int number, Supplier s) {
        System.out.println("The number you provided is: " + number);
        s.get();
    }

    /**
     * 如果不提供结果，就不要用Supplier，所以不使用Supplier<Void>
     * @param number
     * @param supplier
     */
    private void printANumber23(int number, Supplier<Void> supplier) {
        System.out.println("The number you provided is: " + number);
        supplier.get();
    }

    private void printANumber3(int num, Function<Void, Void> function) {
        System.out.println("The number you provided is: " + num);
    }

    private void printFinishMessage() {
        System.out.println("I have finished printing numbers.");
    }

    private void event() {
//        printANumber(6, () -> printFinishMessage());

        printANumber11(6, (s) -> printFinishMessage());

//        printANumber23(6, () -> printFinishMessage());

//        printANumber3(6, () -> printFinishMessage());
//        printANumber(6, this::printFinishMessage);
    }

    @Test
    public void t2() {
        event();
    }



    @Test
    public static void t1() {
        ArrayList<Integer> evens = new ArrayList<>();
        evens.add(10);
        evens.add(20);
        evens.add(30);
        evens.add(40);
        // passing lambda as a parameter
        evens.forEach( (n) -> { System.out.println(n); } );
    }

    public static void main(String[] args) {
        // Passing method reference as a parameter
        // 方法引用
        Thread thread = new Thread(Event::show);
        thread.start();
    }

    public static void show() {
        System.out.println("My Thread");
    }

}
